<style>
    /* 限制 来源 列的宽度 */
    tr>*:nth-child(7),
    td>*:nth-child(7) {
        max-width: 100px;
    }

    /* 限制 哈希 列的宽度 */
    tr>*:nth-child(9),
    td>*:nth-child(9) {
        max-width: 100px;
    }

    /* 限制 时间 列的宽度 */
    tr>*:nth-child(10),
    td>*:nth-child(10) {
        max-width: 100px;
    }
</style>
<p>
    <a href="<%= pageUrl %>object-manage">文件对象管理</a>
    &nbsp;
    <a href="<%= pageUrl %>upload"target="_blank">上传文件</a><br>
    删除文件前必须先删除该文件关联的所有文件对象
</p>
<script>
    list({
        tableElementId: "book-table"
    });

    function stringifyFileSize(nBytes = 0) {
        // 美化输出文件大小
        let sOutput = nBytes + " bytes";
        const aMultiples = ["KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
        for (nMultiple = 0, nApprox = nBytes / 1024; nApprox > 1; nApprox /= 1024, nMultiple++) {
            sOutput = nApprox.toFixed(2) + " " + aMultiples[nMultiple];
        }
        return sOutput;
    }

    function list({ tableElementId }) {
        postRequest("/file/list", { token: localStorageUtils.getToken() })
            .then(function (responseData) {
                var axiosData = responseData.data;
                var status = axiosData.status;
                var data = axiosData.data;
                if (status === "success") {
                    // console.log(data);

                    // 数据进行转换
                    var renderData = [];
                    data.forEach(element => {
                        console.log(element);

                        var watermarkAndAdvertising = [];
                        if (element.watermark)
                            watermarkAndAdvertising.push("有水印");
                        if (element.advertising)
                            watermarkAndAdvertising.push("有广告");
                        watermarkAndAdvertising = watermarkAndAdvertising.join("，") || "无";

                        renderData.push({
                            编号: `${element.id}`,
                            关联书籍: element.bookId === 0 ? "<span style=\"font-size: 10px;\">未关联书籍</span>" : `<span class="overflow-omit" style="margin: 0 auto;">
                                        ID: ${element.bookId}
                                        <a href="/book?id=${element.bookId}">查看</a>
                                        <a href="/dashboard/admin/book-manage/detail?id=${element.bookId}">修改</a>
                                    </span>`,
                            文件名: `${element.fileName}`,
                            格式: `${element.fileExt}`,
                            页数: `${element.numberOfPages ?element.numberOfPages : ""}`,
                            "水印&广告": `<span style="font-size: 10px;">${watermarkAndAdvertising}</span>`,
                            来源: `<span class="overflow-omit" style="font-size: 10px;">${element.source}</span>`,
                            大小: `${stringifyFileSize(element.fileSize)}`,
                            "哈希(双击全选)": `<span class="overflow-omit" style="font-size: 10px;">${element.fileSha1}</span>`,
                            时间: `<span class="overflow-omit" style="font-size: 12px; line-height: 1.2em; display: block;">
                                        ${new Date(element.fileCreateAt).toLocaleString()}
                                    </span>`,
                            管理: `<span style="margin: 0 auto;">
                                <a href="<%= pageUrl %>detail?id=${element.id}">详情</a>
                                <a href="<%= pageUrl %>get-share-url?fileId=${element.id}">添加网盘链接</a><br>
                                <a href="javascript:deleteFile(${element.id});">删除(TODO)</a>
                                    </span>`,
                        })
                    });
                    if (renderData.length == 0) {
                        renderTable({ data: `暂无文件`, tableId: tableElementId, renderTableHead: true });
                    } else {
                        renderTable({ data: renderData, tableId: tableElementId, renderTableHead: true });
                    }
                } else {
                    swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`);
                }
            }).catch(function (error) {
                console.log(error);
                swal("无法连接到服务器，请检查网络连接！");
            });
    }

    // function deleteFile(deleteFileId) {
    //     if (!confirm(`确认要删除编号为 ${deleteFileId} 的文件吗？`)) return;

    //     postRequest("/book/delete", { token: localStorageUtils.getToken(), id: deleteFileId })
    //         .then(function (responseData) {
    //             var axiosData = responseData.data;
    //             var status = axiosData.status;
    //             var data = axiosData.data;
    //             if (status === "success") {
    //                 console.log(data)
    //                 if (data == "success") {
    //                     search({
    //                         tableElementId: "book-table",
    //                         searchText: $("#searchInput").val(),
    //                         categoryId: null
    //                     });
    //                 } else {
    //                     swal("删除失败！");
    //                 }
    //             } else {
    //                 swal(`出错啦！${data.errMsg} (错误码: ${data.errCode})`);
    //             }
    //         }).catch(function (error) {
    //             console.log(error);
    //             swal("无法连接到服务器，请检查网络连接！");
    //         });
    // }
</script>